//
//  PayTypeAlertView.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/2/6.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "PayTypeAlertView.h"


@interface PayTypeAlertView()

@property (strong, nonatomic) NSMutableArray *dataArray;
@property (copy, nonatomic) ButtonBlock buttonBlock;


@end

@implementation PayTypeAlertView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectIndex =0;
    self.dataArray =@[@{@"imageName":@"zhifubao", @"title":@"支付宝支付"},
                      @{@"imageName":@"weixin", @"title":@"微信支付"}].mutableCopy;
    
    self.headerView =[PayTitleView loadNib:@"PayTitleView" frame:CGRectZero];
    [self.headerView didButtonBlock:^(UIButton *button) {
        if (self.buttonBlock) {
            self.buttonBlock(button);
        }
    }];
    self.tableView.tableHeaderView =self.headerView;
    self.footerView =[self creactFooterView];
    self.tableView.tableFooterView =self.footerView;
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    if (_selectIndex !=selectIndex) {
        _selectIndex =selectIndex;
        [_tableView reloadData];
    }
}

- (UIView *)creactFooterView {
    UIView *footerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 60)];
    footerView.backgroundColor =[UIColor clearColor];

    UIView *lineView =[[UIView alloc]initWithFrame:CGRectZero];
    lineView.backgroundColor =[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
    [footerView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(footerView.mas_left).offset(10);
        make.right.mas_equalTo(footerView.mas_right).offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"去支付" forState:UIControlStateNormal];
    button.backgroundColor =APP_DEFAULT_COLOUR;
    button.cornerRadius =22;
    [button addTarget:self action:@selector(payButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(footerView.mas_bottom).offset(0);
        make.left.mas_equalTo(footerView.mas_left).offset(20);
        make.right.mas_equalTo(footerView.mas_right).offset(-20);
        make.height.mas_equalTo(44);
    }];
    
    return footerView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.headerView.frame =CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 100);
    self.footerView.frame =CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 60);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PayTitleCell *cell =[tableView dequeueReusableCellWithIdentifier:@"PayTitleCell"];
    if (!cell) {
        cell =[PayTitleCell findCellWithIdentifier:@"PayTitleCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    NSDictionary *contentDict =_dataArray[indexPath.row];
    [cell.leftImageBtn setBackgroundImage:[UIImage imageNamed:contentDict[@"imageName"]] forState:UIControlStateNormal];
    cell.titleLabel.text =contentDict[@"title"];

    if (indexPath.row ==self.selectIndex) {
        cell.rightButton.selected =YES;
    } else {
        cell.rightButton.selected =NO;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectIndex =indexPath.row;
}

- (void)payButtonClick:(UIButton*)button {
    if (self.buttonBlock) {
        self.buttonBlock(button);
    }
}

- (void)didButtonClickBlock:(ButtonBlock)newBlock {
    self.buttonBlock = newBlock;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
